<?php

class Authenticate
{
	const NOT_LOGIN = 1001;
	const EMAIL_NOT_FOUND = 1002;
	const PASSWORD_INVALID = 1003;
	const ACCOUNT_NOT_VALIDATE = 1004;
	
	const TYPE_USER = 1;
	const TYPE_MARKETING = 2;
	const TYPE_ACCOUNTING = 3;
	const TYPE_TECHNIC = 4;
	
	static private $_errorNo = 0;
	static private $_error = '';
	
	private function __construct(){}
	
	static public function getError()
	{
		return self::$_error;
	}
	
	static public function getErrorNo()
	{
		return self::$_errorNo;
	}
	
	static public function isLogin()
	{
		if(!isset($_SESSION['id']))
            return isset($_SESSION['remoteId']);
        return true;
	}
	
	static public function loginById($id)
	{
		$db = Db::getInstance();
		try{
			$sql = sprintf("SELECT id,email FROM Account WHERE id='%s'", $db->real_escape_string($id));
			$result = $db->query($sql);
			if(!$result){
				throw new Exception($db->error);
			}
			if($result->num_rows == 0){
				throw new Exception('This account id not found');
			}
			$account = $result->fetch_assoc();
			$_SESSION['id'] = $account['id'];
			$_SESSION['email'] = $account['email'];
			return true;
		}
		catch (Exception $ex) {
			Log::error($ex->getMessage());
			return false;
		}
	}
	
	static public function login($email, $password)
	{
		$db = Db::getInstance();
		try{
			$sql = sprintf("SELECT * FROM Account WHERE email='%s' LIMIT 1",
					$db->real_escape_string($email));
			$result = $db->query($sql);
			if($result->num_rows == 0){
				throw new Exception('Email not found', self::EMAIL_NOT_FOUND);
			}
			
			$arr = $result->fetch_assoc();
			if($arr['password'] != md5($password)) {
				throw new Exception('Password invalid', self::PASSWORD_INVALID);
			}
			
			if($arr['status'] == 0) {
				throw new Exception('This account not activate', self::ACCOUNT_NOT_VALIDATE);
			}
			$_SESSION['id'] = $arr['id'];
			$_SESSION['email'] = $arr['email'];
		}
		catch(Exception $ex) {
			self::$_errorNo = $ex->getCode();
			self::$_error = $ex->getMessage();
		}
	}
	
	static public function logout()
	{
		unset($_SESSION['id']);
		unset($_SESSION['email']);
	}
	
	static public function getEmail()
	{
		if(!empty($_SESSION['email'])){
			return $_SESSION['email'];
		}
	}
	
	static public function getId()
	{
		if(!empty($_SESSION['id']))
			return $_SESSION['id'];
        else if(!empty($_SESSION['remoteId']))
            return $_SESSION['remoteId'];
	}
	
	static public function getAdminType()
	{
		return isset($_SESSION['adminType']) ? $_SESSION['adminType']: false;
	}
	
	static public function adminLogin($username, $password)
	{
		//if($username == 'marketing@p2dc.com' && $password == 'p2dc1793'){
		//	$_SESSION['adminType'] = self::TYPE_MARKETING;
		//}
		if($username == 'accounting@p2dc.com' && $password == 'pla20357'){
			$_SESSION['adminType'] = self::TYPE_ACCOUNTING;
		}
		else if($username == 'technic@p2dc.com' && $password == 'pla20357'){
			$_SESSION['adminType'] = self::TYPE_TECHNIC;
		}
		else{
			unset($_SESSION['adminType']);
			return false;
		}
		return true;
	}
	
	static public function adminLogout()
	{
		unset($_SESSION['isAmin']);
		unset($_SESSION['adminType']);
	}
	
	static public function requireAccounting()
	{
		if(self::getAdminType() != self::TYPE_ACCOUNTING){
			header("Location: old.php");
			exit();
		}
	}
	
	static public function requireTechnic()
	{
		if(self::getAdminType() != self::TYPE_TECHNIC){
			header("Location: old.php");
			exit();
		}
	}
	
	static public function requireUser()
	{
		if(!self::isLogin()){
			header("Location: old.php");
			exit();
		}
	}
	
	static public function requireType()
	{
		$args = func_get_args();
		foreach($args as $key => $value){
			if(self::getAdminType()==$value)
				return true;
		}
		header("Location: old.php");
		exit();
	}

    static public function remoteUser($id)
    {
        $_SESSION['remoteId'] = $id;
    }

    static public function isRemote()
    {
        return isset($_SESSION['remoteId']);
    }

    static public function logoutRemote()
    {
        unset($_SESSION['remoteId']);
    }
}